﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class BaseData_NewsEdit : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }

    private string Code
    {
        get
        {
            if (Request.QueryString["code"] != null)
            {
                return Request.QueryString["code"].ToString();
            }
            else
            {
                return "";
            }
        }
    }

    private Model.NEWS Property
    {
        get
        {
            Model.NEWS property = null;
            if (ViewState["Property"] != null)
            {
                property = (Model.NEWS)ViewState["Property"];
            }
            return property;
        }
        set
        {
            ViewState["Property"] = value;
        }
    }


    private bool IsAdd
    {
        get
        {
            return Convert.ToBoolean(ViewState["IsAdd"]);
        }
        set
        {
            ViewState["IsAdd"] = value;
        }

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //Check.CheckPermission("200300104");
        if (!IsPostBack)
        {
            Init_Type();
            if (this.Code != "")
            {
                Label1.Text = "编辑新闻管理";
                this.Property = new BLL.NEWS().GetModel(int.Parse(this.Code));
                if (this.Property != null)
                {
                    txtCode.Text = Property.NEW_ID.ToString();
                    txtName.Text = Property.NEW_TITLE;
                    DDL_Type.SelectedValue = Property.NEW_TYPE.ToString();
                    CKB_effect.Checked = Property.NEW_ISDEL == "1" ? false : true;
                    CKB_Index.Checked = Property.NEW_ISINDEX == "1" ? true : false;
                    Ckb_Top.Checked = Property.NEW_ISTOP == "1" ? true : false;
                    m_myWebTextPane.Text = Property.NEW_CONTENT;
                    txtOrder.Text = Property.NEW_ORDER.ToString();
                    this.IsAdd = false;
                    btnAddOrUpdate.Text = "更新";
                }
                else
                {
                    Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('系统中无此记录!');location.href='News.aspx';</script>");
                }
            }
            else
            {
                this.IsAdd = true;
                btnAddOrUpdate.Text = "添加";
            }
            btnCancel.Attributes.Add("onclick", "if(!confirm('要取消" + btnAddOrUpdate.Text + "并返回上一页吗?'))return false;");
        }
    }

    protected void btnAddOrUpdate_Click(object sender, EventArgs e)
    {
        if (this.IsAdd == true)
        {
            if (true)//Check.CheckPermission("200300104", "00001")
            {
                if (txtName.Text.Trim().Length < 1)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('标题不能为空')", true);
                    return;
                }
                if (m_myWebTextPane.Text.Length < 1)
                {
                    //ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('内容不能为空')", true);
                    //return;
                }
                Model.NEWS property = new Model.NEWS();                
                property.NEW_TYPE = int.Parse(DDL_Type.SelectedValue);
                property.NEW_TYPENAME = DDL_Type.SelectedItem.Text;
                property.NEW_TITLE = txtName.Text.Trim();
                property.NEW_ISTOP = Ckb_Top.Checked ? "1" : "0";
                property.NEW_ISINDEX = CKB_Index.Checked ? "1" : "0";
                property.NEW_CONTENT = m_myWebTextPane.Text;
                property.NEW_ISDEL = CKB_effect.Checked ? "0" : "1";
                property.NEW_ORDER = int.Parse(txtOrder.Text.Trim());
                if (new BLL.NEWS().Add(property) > 0)
                {
                    //Share.InsertLog("200300104", "00001");
                    Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('添加记录成功');location.href='News.aspx';</script>");
                }
                else
                {
                    Share.Alert(this.Page, "添加记录失败,请重新添加。");
                }

            }
            else
            {
                Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('无权进行此操作');location.href='News.aspx';</script>");
            }
        }
        else
        {
            if (true)//Check.CheckPermission("200300104", "00002")
            {
                if (txtName.Text.Trim().Length < 1)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('标题不能为空')", true);
                    return;
                }
                if (m_myWebTextPane.Text.Length < 1)
                {
                    //ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('内容不能为空')", true);
                    //return;
                }
                Property.NEW_ID = int.Parse(txtCode.Text);
                Property.NEW_TYPE = int.Parse(DDL_Type.SelectedValue);
                Property.NEW_TYPENAME = DDL_Type.SelectedItem.Text;
                Property.NEW_TITLE = txtName.Text.Trim();
                Property.NEW_ISTOP = Ckb_Top.Checked ? "1" : "0";
                Property.NEW_ISINDEX = CKB_Index.Checked ? "1" : "0";
                Property.NEW_ISDEL = CKB_effect.Checked ? "0" : "1";
                Property.NEW_CONTENT = m_myWebTextPane.Text;
                Property.NEW_ORDER = int.Parse(txtOrder.Text.Trim());
                if (new BLL.NEWS().Update(this.Property))
                {
                    //Share.InsertLog("200300104", "00002");
                    Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('更新记录成功');location.href='News.aspx';</script>");
                }
                else
                {
                    Share.Alert(this.Page, "更新记录失败,请重新编辑更新。");
                }
            }
            else
            {
                Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('无权进行此操作');location.href='News.aspx';</script>");
            }

        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("News.aspx");
    }
    private void Init_Type()
    {
        List<Model.NewType> list_type = new BLL.NewType().GetAllList();
        List<Model.NewType> newlist = new List<Model.NewType>();
        foreach (Model.NewType nt in list_type)
        {
            if (nt.NT_ISSTOP != "1")
            {
                newlist.Add(nt);
            }
        }
        DDL_Type.DataSource = newlist;
        DDL_Type.DataTextField = "NT_NAME";
        DDL_Type.DataValueField = "NT_ID";
        DDL_Type.DataBind();
    }
}
